package com.fly.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "AjaxCrossFilter",urlPatterns = "/*")
public class AjaxCrossFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletResponse response = (HttpServletResponse) resp;
        ((HttpServletResponse)resp).addHeader("Access-Control-Allow-Headers","Accept, Origin, XRequestedWith, Content-Type, LastModified,Authorization");//允许请求头方式访问



        response.addHeader("Access-Control-Allow-Origin","*");//允许所有来源访问
        response.addHeader("Access-Control-Allow-Method","POST,GET,OPTIONS");//允许访问的方式
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
